import {request} from "@@/exports";
import {ResponseStructure} from "@/requestErrorConfig";
import {ParamsType} from "@ant-design/pro-components";

// 通用请求函数（Pay专用）
async function payBaseRequest<T>(
  method: string,
  url: string,
  data?: any,
  params?: ParamsType,
  options?: Record<string, any>
): Promise<ResponseStructure & { data?: T }> {
  try {
    const result = await request<ResponseStructure & { data?: T }>(`/pay${url}`, {
      method,
      data,
      params,
      ...(options || {}),
    });
    return result;
  } catch (e) {
    console.error(`Pay request error: ${e.message}`);
    return {
      success: false,
      message: e.message,
    } as any;
  }
}

// ==================== GET 请求 ====================
export async function payGet<T>(
  url: string,
  params?: ParamsType,
  options?: Record<string, any>
) {
  return payBaseRequest<T>("GET", url, undefined, params, options);
}

export async function payPageData<T>(
  url: string,
  params?: ParamsType,
  options?: Record<string, any>
) {
  return payBaseRequest<T>("GET", `${url}/page`, undefined, params, options);
}

export async function payListData<T>(
  url: string,
  params?: ParamsType,
  options?: Record<string, any>
) {
  return payBaseRequest<T>("GET", `${url}/list`, undefined, params, options);
}

export async function payGetById<T>(
  url: string,
  id: string | number,
  options?: Record<string, any>
) {
  return payBaseRequest<T>("GET", `${url}/${id}`, undefined, undefined, options);
}

// ==================== POST 请求 ====================
export async function payAddData<T>(
  url: string,
  data: T,
  options?: Record<string, any>
) {
  return payBaseRequest<T>("POST", url, data, undefined, options);
}

// ==================== PUT 请求 ====================
export async function payUpdate<T>(
  url: string,
  data: T,
  options?: Record<string, any>
) {
  return payBaseRequest<T>("PUT", url, data, undefined, options);
}

export async function payUpdateById<T>(
  url: string,
  data: T,
  options?: Record<string, any>
) {
  return payBaseRequest<T>("PUT", `${url}/updateById`, data, undefined, options);
}

// ==================== DELETE 请求 ====================
export async function payDelete(
  url: string,
  data?: any,
  options?: Record<string, any>
) {
  return payBaseRequest("DELETE", url, data, undefined, options);
}

export async function payDeleteById(
  url: string,
  id: string | number,
  options?: Record<string, any>
) {
  return payBaseRequest("DELETE", `${url}/${id}`, undefined, undefined, options);
}


// ==================== 快速POST请求 ====================
/**
 * 快速发起POST请求（简化版）
 * @param url 请求地址（不需要包含/pay前缀）
 * @param data 请求体数据
 * @param params URL参数
 * @param options 额外配置
 */
export async function payPost<T>(
  url: string,
  data: any,
  params?: ParamsType,
  options?: Record<string, any>
): Promise<ResponseStructure & { data?: T }> {
  return payBaseRequest<T>("POST", url, data, params, options);
}

/**
 * 快速发起JSON格式POST请求
 * @param url 请求地址
 * @param jsonData JSON格式的请求体
 * @param options 额外配置
 */
export async function payPostJson<T>(
  url: string,
  jsonData: any,
  options?: Record<string, any>
): Promise<ResponseStructure & { data?: T }> {
  return payPost<T>(url, jsonData, undefined, {
    headers: {
      'Content-Type': 'application/json',
    },
    ...options,
  });
}

/**
 * 快速发起FormData POST请求
 * @param url 请求地址
 * @param formData FormData对象
 * @param options 额外配置
 */
export async function payPostForm<T>(
  url: string,
  formData: FormData,
  options?: Record<string, any>
): Promise<ResponseStructure & { data?: T }> {
  return payPost<T>(url, formData, undefined, {
    headers: {
      'Content-Type': 'multipart/form-data',
    },
    ...options,
  });
}
